﻿<?php
$path = "../";
include_once($path.'configuration.php');
/*traitements*/
include_once($path.'head.php');

if(isset($_GET['action']))
	$action = $_GET['action'];
else
	$action='add';

if($action=="add"){
	if(isset($_POST) && isset($_POST['submit']) && $_POST['submit'] != ''){
		extract($_POST);
		if(isset($nom) && $nom != '' && isset($ip) && $ip != '' && preg_match("#\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b#",$ip)){
			if($dbh->getMachineByIP($ip)==0){
				$machine = new machine($dbh);
				$machine->setNom($nom);
				$machine->setIp($ip);
				$machine->setSalle($salle);
				$machine->save();
				header('Location: '.$path . 'administration/gestion_machine.php');
				exit;
			}else{
				$message="Votre adresse IP doit être unique !";
			}
		}else{
			$message = "Veuillez remplir tous les champs obligatoires !";
		}
	}
}elseif($action == "modif"){
	if(isset($_GET) && isset($_GET['machine']) && $_GET['machine'] != 0){
		if(isset($_POST) && isset($_POST['submit']) && $_POST['submit'] != ''){
			extract($_POST);

			if(isset($nom) && $nom != '' && isset($ip) && $ip != '' && preg_match("#\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b#",$ip)){
				if($dbh->getMachineByIP($ip, $_GET['machine'])==0){
					$machine = new machine($dbh, $_GET['machine']);
					$machine->setNom($nom);
					$machine->setIp($ip);
					$machine->setSalle($salle);
					$machine->save();
					header('Location: '.$path . 'administration/gestion_machine.php');
					exit;
				}else {
					$message="l'IP existe déja, veuillez en saisir une autre !";
				}
			}else{
				$message = "Veuillez remplir tous les champs obligatoires !";
			}
			
		}
	}	
}
elseif($action == "delete"){
	if(isset($_GET) && isset($_GET['machine']) && $_GET['machine'] != 0){
		$machine = new machine($dbh, $_GET['machine']);
		$machine->delete();
		header('Location: '.$path .'administration/gestion_machine.php');
	}
}

	
if($action == "modif"){
	if(isset($_GET) && isset($_GET['machine']) && $_GET['machine'] != 0){
		$user = new machine($dbh, $_GET['machine']);
		$nom = $user->getNom();
		$ip = $user->getIp();
		$salle = $user->getSalle()->getId();
	}
}
?>

<div id="bodyWrapper">
	<script type="text/javascript">
	$(function() {
		$('#form').submit(function(){
			var error = false;
			var message = 'Erreur : <br/>';
			
			if($("#nom").val() == ''){
				message += 'Choisissez un nom ! <br/>';
				$("#nom").addClass( "error_input" )
				error = true;
			}else{
				$("#nom").removeClass( "error_input" )
			}

			var reg = new RegExp('^\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b$');
			
			if($("#ip").val() == '' || !reg.test($("#ip").val())){
				message += 'Choisissez une IP valide ! <br/>';
				$("#ip").addClass( "error_input" )
				error = true;
			}else{
				$("#ip").removeClass( "error_input" )
			}
			
			if(error == true){
				$("#dialog").html('<p>'+message+'</p>');
				$("#dialog").dialog("open");
				return false;
			}
		});
	});
	</script>
	<?php if(isset($message) && $message != ''){?>
	<script type="text/javascript" >
		$(function() {
			
			$("#modular_dialog").dialog({
				autoOpen: true,
				show: {
					effect: "explode",
					duration: 1000
				},
				hide: {
					effect: "explode",
					duration: 1000
				},
				modal: true
			});
			
		});
	</script>
	<div id="modular_dialog">
	<p><?php echo $message;?></p>
	</div>
	<?php } ?>
	<form method="post" id="form" action="">
		<p>
			<label for="name" class="label">Nom* :</label> <input type="text"
				id="nom" name="nom" class="input"
				value="<?php if(isset($nom)) echo $nom;?>" />
		</p>

		<p>
			<label for="salle" class="label">salle* :</label>
			<select class="input" name="salle" id="salle">
				<option value="">Pas de salle</option>
				<?php
					$salles = $dbh->getAllSalles();
					foreach($salles as $value){
				?>
					<option value="<?php echo $value['id_salle'];?>" <?php if(isset($salle) && $salle != '' && $value['id_salle'] == $salle){echo 'selected="selected"';} ?>><?php echo $value['nom_salle'];?></option>
				<?php
					} 
				?>
			</select>
		</p>
		<p>
			<label for="ip" class="label">IP* :</label> <input type="text"
				id="ip" name="ip" class="input"
				value="<?php if(isset($ip)) echo $ip;?>" />
		</p>
		<p>
			<input type="submit" name="submit" id="submit" class="input"
				value="Envoyer" /></p>
	</form>
</div>
<?php
include_once($path.'footer.php');
?>